/* global window */
import { message } from 'antd'
import api from 'api'
import modelExtend from 'dva-model-extend'
import { pathMatchRegexp } from 'utils'
import { pageModel } from 'utils/model'
import moment from 'moment'

const {
  queryStrategy,
  createStrategy,
  updateStrategy,
  deleteStrategy,
  projects,
  devices,
  queryTodayMsg,
  fetchdetail,
  InvokeMission,
} = api

const regionstore = {
  projects: [],
  deviceIds: {},
  Note: '',
  Repeat: 0,
  //
  //
  searchedData: {},
  //
  checkedKeys: [], //选择的Imei
  checkedCmdKeys: undefined,
  choosedCmds: {}, //{LIGHT:{}}
  //
  // EditingStrategy: undefined,
  EditStrategyShow: false,

  AllStrategy: [],
  nowKey: 'create',
  //
  // FinishedStrategy: undefined
  // waitUpLoadStrategy: undefined
}

const tmpEditingStrategy = {
  // type: Strategy
  // Name: 如果没
  // Note
  // objectId: xxx 如果没有说明是本地创建，如果有就是已有的策略
  // Datas: [{ 0, 1, 2}, [2, 2, 3], [3, 2, 3]]
}

export default modelExtend(pageModel, {
  namespace: 'multicmd',

  state: {
    ...regionstore,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (pathMatchRegexp(`/multicmd`, location.pathname)) {
          // let payload = { index: 0, start: '', end: '' }
          // let todaydate = moment().format('YYYY-MM-DD')
          // if (location.query) {
          //     if (location.query.index !== undefined) {
          //         payload.index = location.query.index
          //     }
          //     if (location.query.start) {
          //         payload.start = location.query.start
          //     } else {
          //         payload.start = moment()
          //             .subtract(1, 'month')
          //             .format('YYYY-MM-DD')
          //     }
          //     if (location.query.end) {
          //         payload.end = location.query.end
          //     } else {
          //         payload.end = todaydate
          //     }
          // }
          // dispatch({
          //   type: 'queryProjects',
          // })
        }
      })
    },
  },

  effects: {
    *clearstore({ payload = {} }, { call, put, select }) {},
    *queryStrategy({ payload }, { call, put, select }) {
      const { result } = yield call(queryStrategy)
      yield put({
        type: 'updateState',
        payload: { AllStrategy: result },
      })
    },
    *updateStrategy({ payload }, { call, put, select }) {
      message.loading('更新策略中...')
      const { result } = yield call(updateStrategy, {
        params: JSON.stringify(payload),
      })
      message.destroy()
      message.success('更新成功！')
    },
    *createStrategy({ payload }, { call, put, select }) {
      message.loading('创建策略中...')
      const { result } = yield call(createStrategy, {
        params: JSON.stringify(payload),
      })
      message.destroy()
      message.success('创建成功！')
    },
    *deleteStrategy({ payload }, { call, put, select }) {
      message.loading('删除策略中...')
      const { result } = yield call(deleteStrategy, payload)
      message.destroy()
      message.success('删除成功！')
    },
    *queryDevices({ payload }, { call, put, select }) {
      const { resolve, project } = payload
      const { result } = yield call(devices, { project, page: 1, pageSize: -1 })
      const { deviceIds } = yield select(_ => _.multicmd)
      deviceIds[project] = result.data
      yield put({
        type: 'updateState',
        payload: { deviceIds },
      })
      resolve()
    },
    *queryProjects({ payload }, { call, put }) {
      const { result } = yield call(projects, payload)
      if (result) {
        yield put({ type: 'updateState', payload: { projects: result.data } })
      }
    },
    *InvokeMission({ payload }, { call, put, select }) {
      message.loading('注册任务中...')
      const {
        checkedKeys,
        choosedCmds,
        checkedCmdKeys,
        Note,
        Repeat,
      } = yield select(_ => _.multicmd)
      const Strategy = {}
      Strategy[checkedCmdKeys] = choosedCmds[checkedCmdKeys]
      const { result } = yield call(InvokeMission, {
        strategyString: JSON.stringify({
          Targets: checkedKeys,
          Strategy,
          Note,
          Repeat: Number(Repeat),
        }),
      })
      message.destroy()
      message.success('执行成功！')
    },
  },

  reducers: {},
})
